package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.access.EntityMjZoneDate;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoZoneDate;
import com.ocom.srvpaccess.service.ZoneDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ZoneDateServiceImpl extends ServiceImpl<IDaoZoneDate, EntityMjZoneDate> implements ZoneDateService {

    @Autowired
    private IDaoZoneDate iDaoZoneDate;

    //从分库门禁时区设置表mj_zone_date查找满足条件的记录
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjZoneDate> getZoneTime(Long companyId) {
        QueryWrapper<EntityMjZoneDate> query = Wrappers.query();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        query.and(wrapper -> {
                wrapper.eq("day_date", 0);
                wrapper.or(itemWrapper ->{
                    itemWrapper.ge("date", DateUtils.getDate());
                    itemWrapper.eq("day_date", 1);
                });
            });
        query.orderByDesc("date");
        return iDaoZoneDate.selectList(query);
    }

    //从门禁时区设置表mj_zone_date查找com_id= companyID、date_day= dateDay、date= accessDay的记录,
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public EntityMjZoneDate getZoneDateInfo(Long companyId,Integer dateDay, String weekDay) {
        QueryWrapper<EntityMjZoneDate> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("day_date",dateDay);
        query.eq("date",weekDay);
        query.last("limit 1");
        return iDaoZoneDate.selectOne(query);
    }
}
